#include "StdAfx.h"
#include "WindowMove.h"

WindowMove::WindowMove(void)
: _wnd(NULL)
{
}

WindowMove::~WindowMove(void)
{
}

CWnd *WindowMove::getWnd()
{
	return _wnd;
}

void WindowMove::setWnd(CWnd *wnd)
{
	_wnd = wnd;
}

void WindowMove::setLeft(int direct, int type, int param)
{
	_leftDirect = direct;
	_leftType = type;
	_leftParam= param;
}

void WindowMove::setTop(int direct, int type, int param)
{
	_topDirect = direct;
	_topType = type;
	_topParam= param;
}

void WindowMove::setRight(int direct, int type, int param)
{
	_rightDirect = direct;
	_rightType = type;
	_rightParam= param;
}

void WindowMove::setBottom(int direct, int type, int param)
{
	_bottomDirect = direct;
	_bottomType = type;
	_bottomParam= param;
}

void WindowMove::resize()
{
	if (_wnd == NULL || _wnd->GetSafeHwnd() == NULL)
	{
		return;
	}

	CWnd *parent = _wnd->GetParent();

	if (parent == NULL || parent->GetSafeHwnd() == NULL)
	{
		return;
	}

	RECT rt;
	RECT rt2;
	ZeroMemory(&rt, sizeof(rt));
	ZeroMemory(&rt2, sizeof(rt2));
	parent->GetClientRect(&rt);
	_wnd->GetWindowRect(&rt2);
	parent->ScreenToClient(&rt2);

	int width = rt.right - rt.left;
	int height= rt.bottom - rt.top;

	switch (_leftDirect)
	{
	case WINDOW_MOVE_DIRECT_LEFT:
		{
			switch (_leftType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.left = _leftParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.left = int(width * 1.0 / 10000 * _leftParam);
				break;
			default:
				break;
			}
		}
		break;
	case WINDOW_MOVE_DIRECT_RIGHT:
		{
			switch (_leftType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.left = rt.right + _leftParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.left = rt.right + int(width * 1.0 / 10000 * _leftParam);
				break;
			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	switch (_topDirect)
	{
	case WINDOW_MOVE_DIRECT_TOP:
		{
			switch (_topType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.top = _topParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.top = int(height * 1.0 / 10000 * _topParam);
				break;
			default:
				break;
			}
		}
		break;
	case WINDOW_MOVE_DIRECT_BOTTOM:
		{
			switch (_topType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.top = rt.bottom + _topParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.top = rt.bottom + int(height * 1.0 / 10000 * _topParam);
				break;
			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	switch (_rightDirect)
	{
	case WINDOW_MOVE_DIRECT_LEFT:
		{
			switch (_rightType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.right = _rightParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.right = int(width * 1.0 / 10000 * _rightParam);
				break;
			default:
				break;
			}
		}
		break;
	case WINDOW_MOVE_DIRECT_RIGHT:
		{
			switch (_rightType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.right = rt.right + _rightParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.right = rt.right + int(width * 1.0 / 10000 * _rightParam);
				break;
			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	switch (_bottomDirect)
	{
	case WINDOW_MOVE_DIRECT_TOP:
		{
			switch (_bottomType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.bottom = _bottomParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.bottom = int(height * 1.0 / 10000 * _bottomParam);
				break;
			default:
				break;
			}
		}
		break;
	case WINDOW_MOVE_DIRECT_BOTTOM:
		{
			switch (_bottomType)
			{
			case WINDOW_MOVE_TYPE_PIXEL:
				rt2.bottom = rt.bottom + _bottomParam;
				break;
			case WINDOW_MOVE_TYPE_PERCENT:
				rt2.bottom = rt.bottom + int(height * 1.0 / 10000 * _bottomParam);
				break;
			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	_wnd->MoveWindow(&rt2);
}